import axios from 'axios'

import {Indicator} from 'mint-ui';
/*
// 等待样式
let loading = {
  mask: function () {
    let dom = document.createElement('div')
    dom.className += 'g-mask'
    dom.innerHTML = '<p></p>'
    document.body.appendChild(dom)
  },
  unmask: function () {
    let body = document.querySelector('body')
    body.removeChild(body.querySelector('.g-mask'))
  }
}
*/
axios.defaults.timeout = 10000 // 响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' // 配置请求头
axios.defaults.withCredentials = true
// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
  console.log(config)
  Indicator.open()
  return config
}, (error) => {
  Indicator.close()
  if (Promise.reject) {
    return Promise.reject(error)
  } else {
    console.log('这个接口没有catch')
  }
})

// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) => {

  Indicator.close()
  if (res.data.code !== '200') {
    if (res.data.code === '402') {
      console.log('用户未登录')
    }
    return Promise.reject(res)
  } else {
    return res
  }
}, (error) => {

  Indicator.close()
  return Promise.reject(error)
})
export default axios
